package com.example.peibei.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.HomeBannerCate;
import com.dingtao.common.bean.ReportBean;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.ReportPresenter;
import com.example.peibei.service.presenter.UploadImagePresenter;
import com.example.peibei.ui.adapter.ChooseReportAdapter;
import com.example.peibei.ui.adapter.ChooseWeekTimeAdapter;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ReportActivity extends WDActivity {
    @BindView(R.id.rv_report)
    RecyclerView rv_report;
    @BindView(R.id.iv_img_one)
    ImageView iv_img_one;
    @BindView(R.id.iv_img_two)
    ImageView iv_img_two;
    @BindView(R.id.iv_img_three)
    ImageView iv_img_three;
    @BindView(R.id.iv_img_four)
    ImageView iv_img_four;
    @BindView(R.id.rl_add_one)
    RelativeLayout rl_add_one;
    @BindView(R.id.et_feedback)
    EditText et_feedback;
    @BindView(R.id.ll_imgs)
    LinearLayout ll_imgs;

    private List<ReportBean> reportBeans = new ArrayList<>();
    private ChooseReportAdapter chooseReportAdapter;
    private UploadImagePresenter uploadImagePresenter;
    String imagePathOne = "";
    String imagePathTwo = "";
    String imagePathThree = "";
    String imagePathFour = "";
    private SPUtils spUtils;
    private String token;
    private String report;
    private String mid;
    private ReportPresenter reportPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_report;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        mid = bundle.getString("mid");
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_report.setLayoutManager(new LinearLayoutManager(this));
        chooseReportAdapter = new ChooseReportAdapter(reportBeans);
        rv_report.setAdapter(chooseReportAdapter);
        addReportData();
        chooseReportAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                reportBeans.get(position).setChoosed(!reportBeans.get(position).isChoosed());
                chooseReportAdapter.notifyDataSetChanged();
            }
        });
        uploadImagePresenter = new UploadImagePresenter(new UploadCall());
        reportPresenter = new ReportPresenter(new ReportCall());
    }

    private void addReportData() {
        reportBeans.add(new ReportBean("色情"));
        reportBeans.add(new ReportBean("诈骗"));
        reportBeans.add(new ReportBean("转单"));
        reportBeans.add(new ReportBean("政治"));
        reportBeans.add(new ReportBean("标价与实际不符"));
        reportBeans.add(new ReportBean("侮辱诋毁"));
        reportBeans.add(new ReportBean("广告酒托"));
        reportBeans.add(new ReportBean("涉未成年人有害信息"));
        reportBeans.add(new ReportBean("其他原因"));
        chooseReportAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.rl_add_one)
    public void addImgs() {
        showChooseImageDialog();
    }

    @OnClick(R.id.bt_submit)
    public void submit() {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < reportBeans.size(); i++) {
            if (reportBeans.get(i).isChoosed()) {
                if (i == reportBeans.size() - 1) {
                    str.append(reportBeans.get(i).getName());
                } else {
                    str.append(reportBeans.get(i).getName());
                    str.append(",");
                }
            }
        }
        report = str.toString();
        if (TextUtils.isEmpty(report)) {
            UIUtils.showToastSafe("至少选择一个举报原因");
            return;
        }
        String content = et_feedback.getText().toString();
        String filePath = imagePathOne;
        if (!imagePathTwo.equals("")) {
            filePath = imagePathOne + "," + imagePathTwo;
        }
        if (!imagePathThree.equals("")) {
            filePath = imagePathOne + "," + imagePathTwo + "," + imagePathThree;
        }
        if (!imagePathFour.equals("")) {
            filePath = imagePathOne + "," + imagePathTwo + "," + imagePathThree + "," + imagePathFour;
        }
        reportPresenter.reqeust("Bearer " + token, content, mid, filePath, report);
    }

    private void showChooseImageDialog() {
        PictureSelector
                .create(ReportActivity.this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*结果回调*/
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
            if (data != null) {
                PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                if (pictureBean.isCut()) {
                    uploadImage(pictureBean.getPath());
                }
            }
        }
    }

    private void uploadImage(String imagePath) {
        ll_imgs.setVisibility(View.VISIBLE);
        if (imagePathFour.equals("")) {
            rl_add_one.setVisibility(View.VISIBLE);
        } else {
            rl_add_one.setVisibility(View.GONE);
        }
        if (imagePathOne.equals("")) {
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
            iv_img_one.setImageBitmap(compressQuality(bitmap));
        } else {
            if (imagePathTwo.equals("")) {
                iv_img_two.setImageBitmap(BitmapFactory.decodeFile(imagePath));
            } else {
                if (imagePathThree.equals("")) {
                    iv_img_three.setImageBitmap(BitmapFactory.decodeFile(imagePath));
                } else {
                    iv_img_four.setImageBitmap(BitmapFactory.decodeFile(imagePath));
                }
            }
        }
        uploadImagePresenter.reqeust(imagePath, "Bearer " + token);
    }

    private Bitmap compressQuality(Bitmap bitmap) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos);
        byte[] bytes = bos.toByteArray();
        Log.e("bitmapsize", String.valueOf(bytes.length));
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    }

    class UploadCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            Log.i("answer", "图片上传成功：" + data);
            if (imagePathOne.equals("")) {
                imagePathOne = data;
            } else {
                if (imagePathTwo.equals("")) {
                    imagePathTwo = data;
                } else if (imagePathThree.equals("")) {
                    imagePathThree = data;
                } else {
                    imagePathFour = data;
                }
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    class ReportCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("已收到您的举报，我们会尽快核实情况");
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }

    }

    @Override
    protected void destoryData() {

    }
}
